var img_dims = {};
var argument_regex = /(\w+)\((\w+(?:\W*,\W*\w+)*)\)/;

function load_image(name, obj, final_steps) {
    obj.image = new Image();
    obj.image.src = name;
    if (img_dims[name] !== undefined) {
        obj.image.width = img_dims[name][0];
        obj.image.height = img_dims[name][1];
        obj.width = obj.image.width;
        obj.height = obj.image.height / obj.frameCount;
		$(obj.image).height(obj.height);
		$(obj.image).width(obj.width);
    }
    else {
        obj.image.onload = function() {
			
            obj.width = obj.image.width;
            obj.height = obj.image.height / obj.frameCount;
            if (img_dims[name] === undefined) {
                img_dims[name] = [obj.image.width, obj.image.height];
            }
			$(obj.image).height(obj.height);
			$(obj.image).width(obj.width);
        };
    }
	if (!(final_steps === undefined)) { 
		final_steps();
	}
}

function download_file(uri, callback) {
    
    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", uri, true);
    txtFile.onreadystatechange = function() {
        if (txtFile.readyState === 4) {  // document is ready to parse.
            if (txtFile.status === 200) {  // file is found
                var output = "";
                if (ends_with(uri, "txt")) {
                    var allText = txtFile.responseText; 
                    output = txtFile.responseText.split("\n");
                }
                else if (ends_with(uri, "xml")) {
                    var xml = txtFile.responseXML;
                    output = xml;
                }
                callback(output);
            }
        }
    };
    txtFile.send(null);
}
function log(args) {
    if (window.console) {
        if (console.log) {
            Function.prototype.apply.apply(console.log, [console, arguments]);
        }
    }
}
function force_redraw(element) {
    $(element).hide(5);
    $(element).show(5);
}
function make_centered(string) {
    return "<div class='centered_table'> \n\t<div class='centered_cell'>" + string + "\n\t\t</div> \n\t\t</div>";
}

function parse_function(string, prepend, append) {
    var j, q = 0;
    var name = "";
    var output;
    log(string, prepend, append, "ALL");
    if (prepend === undefined) {
        prepend = [];
    }
    if (append === undefined) {
        append = [];
    }
    var str = replace_all(strip(string, " "), "&", ",");
    if (str.match(argument_regex)) {
        var match = str.match(argument_regex);
        name = match[1];
        log(match, "MATC");
        var args = str.match(argument_regex)[2].split(",");
        for (j = 0; j < args.length; j = j + 1) {
            args[j] = strip(args[j], " ");
        }
        args = [args];
        for (q = prepend.length - 1; q >= 0; q = q - 1) {
            log(prepend[q], prepend, "PREEEE");
            args.unshift(prepend[q]);
        }
        for (q = 0; q < append.length; q = q + 1) {
            args.push(append[q]);
        }
        output = window[name].apply(window[name], args);
    }
    else {
        name = strip(string, " ");
        log(name, "NAME", name==="True");
        if (name) {
            output = window[name]();
        }
        else {
            output = window.True();
        }
    }
    return output;
}

function replace_all(string, old, repl) {
    var cur_str = string;
    var new_str = string.replace(old, repl);
    while (new_str !== cur_str) {
        cur_str = new_str;
        new_str = new_str.replace(old, repl);
        
    }
    return new_str;
}

function strip(string, bad_chars) {
    var left_index = 0;
    var right_index = string.length -1;
    for (left_index = 0; left_index < string.length; left_index = left_index + 1) {
        if (bad_chars.indexOf(string.charAt(left_index)) === -1) {
            break;
        }
    }
    for (right_index = string.length - 1; right_index > -1; right_index = right_index - 1) {
        if (bad_chars.indexOf(string.charAt(right_index)) === -1) {
            break;
        }
    }
    return string.substring(left_index, right_index + 1);
}

function ends_with(string, suffix) {
    //Adapted from http://stackoverflow.com/questions/280634/endswith-in-javascript
    //Thanks chakrit (http://stackoverflow.com/users/3055/chakrit)!
    return string.indexOf(suffix, string.length - suffix.length) !== -1;
}

function fadeOut (thing, speed) {
	var fade_count = 0;
	if (thing.css("opacity")) {
		thing.css("opacity", thing.css("opacity") - 1/speed);
	}
	else {
		thing.css("opacity", 1);
	}
	if (thing.css("opacity") < 1/speed) {
		//setTimeout(function () {fadeOut(thing, speed);}, 50);
	//}
    //else {
        thing.css("opacity", 0);
    }
}